// pages/myArchives/myArchives.js
const app = getApp();
var utils = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sex: '男',//性别
    imgPath: app.d.imgPath,
    trueName: '',//真实姓名
    height: '',//身高
    weight: '',//体重
    rightFoot:'',//右脚
    leftFoot: '',//左脚
    zysrc: [], //足压照片
    zxsrc: [],//足型照片
    xdsrc:[],//鞋垫照片
    zhsrc:[],//后侧足踝照片
    xdisrc:[],//鞋底照片
    tdsrc: [],//痛点&伤口照片
    btsrc:[],//步态照片

    zysrcs: [], //足压照片
    zxsrcs: [],//足型照片
    xdsrcs: [],//鞋垫照片
    zhsrcs: [],//后侧足踝照片
    xdisrcs: [],//鞋底照片
    tdsrcs: [],//痛点&伤口照片
    btsrcs: [],//步态照片

    upload0: true,
    upload1: true,
    upload2: true,
    upload3: true,
    upload4: true,
    upload5: true,
    upload6: true
    
  },
  //选择性别
  choseSex(e) {
    if (e.target.dataset.sex == 0) { //男
      this.setData({
        sex: '男'
      })
    } else {
      this.setData({
        sex: '女'
      })
    }
  },
  //获取真实姓名
  trueNameFn(e){
    this.setData({
      trueName:e.detail.value
    })
  },
  //身高
  heightFn(e){
    this.setData({
      height: e.detail.value
    })
  },
  //体重
  weightFn(e){
    this.setData({
      weight: e.detail.value
    })
  },
  rightFn(e){
    this.setData({
      rightFoot: e.detail.value
    })
  },
  leftFn(e){
    this.setData({
      leftFoot: e.detail.value
    })
  },
  //上传所有信息
  uploadAll(){
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    setTimeout(function () {
      var sucFunc = function (that, res) {
        if (res.data == 200) {
          wx.showModal({
            title: '提示',
            content: '保存成功',
            success(res) {
              wx.redirectTo({
                url: '/pages/myArchives/myArchives'
              })
            }
          })
        } else {
          wx.showModal({
            title: '提示',
            content: '发布失败',
            success(res) {
              return false
            }
          })
        }
        wx.hideLoading();
      }
      var data = { 
        openid: app.d.openid, 
        name: that.data.trueName,
        sex: that.data.sex,
        height: that.data.height,
        weight: that.data.weight,
        rightFoot: that.data.rightFoot,
        leftFoot: that.data.rightFoot,
        zysrc_images: that.data.zysrcs,
        zxsrc_images: that.data.zxsrcs,
        xdsrc_images: that.data.xdsrcs,
        zhsrc_images: that.data.zhsrcs,
        xdisrc_images: that.data.xdisrcs,
        tdsrc_images: that.data.tdsrcs,
        btsrc_images: that.data.btsrcs
      }
      if (!that.data.trueName || !that.data.height || !that.data.weight || !that.data.rightFoot || !that.data.leftFoot || !that.data.zysrcs || !that.data.zxsrcs || !that.data.xdsrcs || !that.data.zhsrcs || !that.data.xdisrcs || !that.data.tdsrcs || !that.data.btsrcs){
        wx.hideLoading();
        wx.showModal({
          title: '提示',
          content: '请将信息填写完整',
          success(res) {
            
            return false
          }
        })
      }else{
        utils.wxRequest(that, app.d.path, "My/baoc", data, sucFunc);
      }
      
    }, 1000)
  },
  //上传图片
  upload(e) {
    var _this = this
    var that = this
    wx.showLoading({
      title: '加载中',
    })
    wx.chooseImage({
      count: 3, // 最多可以选择的图片张数
      sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function (res) { //上传成功发送请求
        let newarr = res.tempFilePaths
        var i = e.currentTarget.dataset.i
        var oldarr = []
        console.log(i)
        if (i == 0) { //点击足压照片
          oldarr = _this.data.zysrc
        } else if (i == 1) {
          oldarr = _this.data.zxsrc
        }else if (i == 2){
          oldarr = _this.data.xdsrc
        } else if (i == 3) {
          oldarr = _this.data.zhsrc
        } else if (i == 4) {
          oldarr = _this.data.xdisrc
        } else if (i == 5) {
          oldarr = _this.data.tdsrc
        } else if (i == 6) {
          oldarr = _this.data.btsrc
        }
        // console.log(oldarr, 'old')
        let imgarr = oldarr.concat(newarr)
        // console.log(imgarr, 'new')
        if (imgarr.length >= 3) {
          let arr = imgarr.splice(0, 3)
          if (i == 0) { //点击足压照片
            _this.setData({
              upload0: false,
              zysrc: arr
            })
          } else if (i == 1) {
            _this.setData({
              upload1: false,
              zxsrc: arr
            })
          } else if (i == 2) {
            _this.setData({
              upload2: false,
              xdsrc: arr
            })
          } else if (i == 3) {
            _this.setData({
              upload3: false,
              zhsrc: arr
            })
          } else if (i == 4) {
            _this.setData({
              upload4: false,
              xdisrc: arr
            })
          } else if (i == 5) {
            _this.setData({
              upload5: false,
              tdsrc: arr
            })
          } else if (i == 6) {
            _this.setData({
              upload6: false,
              btsrc: arr
            })
          }

        } else {
          if (i == 0) { //点击足压照片
            _this.setData({
              upload0: true,
              zysrc: imgarr
            })
          } else if (i == 1) {
            _this.setData({
              upload1: true,
              zxsrc: imgarr
            })
          } else if (i == 2) {
            _this.setData({
              upload2: true,
              xdsrc: imgarr
            })
          } else if (i == 3) {
            _this.setData({
              upload3: true,
              zhsrc: imgarr
            })
          } else if (i == 4) {
            _this.setData({
              upload4: true,
              xdisrc: imgarr
            })
          } else if (i == 5) {
            _this.setData({
              upload5: true,
              tdsrc: imgarr
            })
          } else if (i == 6) {
            _this.setData({
              upload6: true,
              btsrc: imgarr
            })
          }
        }
        wx.uploadFile({
          url: app.d.path + 'My/uploadimg',
          filePath: newarr[0],
          name: 'file',
          success: function (res) {
            console.log(res.data);
            wx.hideLoading();
            var img = res.data
            var imgs = img.replace(/\\/g, "/");
            if(i == 0){
              that.setData({
                zysrcs: that.data.zysrcs.concat(imgs)
              })
            }else if(i == 1){
              that.setData({
                zxsrcs: that.data.zxsrcs.concat(imgs)
              })
            } else if (i == 2) {
              that.setData({
                xdsrcs: that.data.xdsrcs.concat(imgs)
              })
            } else if (i == 3) {
              that.setData({
                zhsrcs: that.data.zhsrcs.concat(imgs)
              })
            } else if (i == 4) {
              that.setData({
                xdisrcs: that.data.xdisrcs.concat(imgs)
              })
            } else if (i == 5) {
              that.setData({
                tdsrcs: that.data.tdsrcs.concat(imgs)
              })
            } else if (i == 6) {
              that.setData({
                btsrcs: that.data.btsrcs.concat(imgs)
              })
            }
            
          }
        })
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })

  },
  imgYu: function (event) {
    var src = event.currentTarget.dataset.src; //获取data-src
    var imgList = event.currentTarget.dataset.list; //获取data-list
    //图片预览
    wx.previewImage({
      current: src, // 当前显示图片的http链接
      urls: imgList // 需要预览的图片http链接列表
    })
  },
  //删除图片
  remove(e) {
    let j = e.currentTarget.dataset.index;
    // console.log(j,'j')
    let arr = [];
    let arrs = [];
    let index = e.currentTarget.dataset.j;
    if (index == 0){
      arr = this.data.zysrc
      arr.splice(j, 1)
      arrs = this.data.zysrcs
      arrs.splice(j, 1)
      this.setData({
        zysrc: arr,
        zysrcs: arrs
      })
      if (arr.length >= 3) {
        this.setData({
          upload0: false
        })
      } else {
        this.setData({
          upload0: true
        })
      }
    }else if(index == 1){
      arr = this.data.zxsrc
      arr.splice(j, 1)
      arrs = this.data.zxsrcs
      arrs.splice(j, 1)
      this.setData({
        zxsrc: arr,
        zxsrcs: arrs
      })
      if (arr.length >= 3) {
        this.setData({
          upload1: false
        })
      } else {
        this.setData({
          upload1: true
        })
      }
    } else if (index == 2) {
      arr = this.data.xdsrc
      arr.splice(j, 1)
      arrs = this.data.xdsrcs
      arrs.splice(j, 1)
      this.setData({
        xdsrc: arr,
        xdsrcs: arrs
      })
      if (arr.length >= 3) {
        this.setData({
          upload2: false
        })
      } else {
        this.setData({
          upload2: true
        })
      }
    } else if (index == 3) {
      arr = this.data.zhsrc
      arr.splice(j, 1)
      arrs = this.data.zhsrcs
      arrs.splice(j, 1)
      this.setData({
        zhsrc: arr,
        zhsrcs: arrs
      })
      if (arr.length >= 3) {
        this.setData({
          upload3: false
        })
      } else {
        this.setData({
          upload3: true
        })
      }
    } else if (index == 4) {
      arr = this.data.xdisrc
      arr.splice(j, 1)
      arrs = this.data.xdisrcs
      arrs.splice(j, 1)
      this.setData({
        xdisrc: arr,
        xdisrcs: arrs
      })
      if (arr.length >= 3) {
        this.setData({
          upload4: false
        })
      } else {
        this.setData({
          upload4: true
        })
      }
    } else if (index == 5) {
      arr = this.data.tdsrc
      arr.splice(j, 1)
      arrs = this.data.tdsrcs
      arrs.splice(j, 1)
      this.setData({
        tdsrc: arr,
        tdsrcs: arrs
      })
      if (arr.length >= 3) {
        this.setData({
          upload5: false
        })
      } else {
        this.setData({
          upload5: true
        })
      }
    } else if (index == 6) {
      arr = this.data.btsrc
      arr.splice(j, 1)
      arrs = this.data.btsrcs
      arrs.splice(j, 1)
      this.setData({
        btsrc: arr,
        btsrcs: arrs
      })
      if (arr.length >= 3) {
        this.setData({
          upload6: false
        })
      } else {
        this.setData({
          upload6: true
        })
      }
    }
    
    

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    var sucFunc = function (that, res) {
      that.setData({
        my: res.data,
        
      })
      if(res.data){
        that.setData({
          sex: res.data.sex,
          trueName: res.data.name,//真实姓名
          height: res.data.height,//身高
          weight: res.data.weight,//体重
          rightFoot: res.data.rightFoot,//右脚
          leftFoot: res.data.leftFoot,//左脚
          zysrc: res.data.zysrc,
          zxsrc: res.data.zxsrc,
          xdsrc: res.data.xdsrc,
          zhsrc: res.data.zhsrc,
          xdisrc: res.data.xdisrc,
          tdsrc: res.data.tdsrc,
          btsrc: res.data.btsrc,
          zysrcs: res.data.zysrcs,
          zxsrcs: res.data.zxsrcs,
          xdsrcs: res.data.xdsrcs,
          zhsrcs: res.data.zhsrcs,
          xdisrcs: res.data.xdisrcs,
          tdsrcs: res.data.tdsrcs,
          btsrcs: res.data.btsrcs,
        })
      }
      
      wx.hideLoading();
    }
    var data = { openid: app.d.openid }
    utils.wxRequest(that, app.d.path, "My/myArchives", data, sucFunc);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})